package com.dlc.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dlc.shop.bean.dto.ShopCustomerDTO;
import com.dlc.shop.bean.model.Order;
import com.dlc.shop.bean.model.ShopCustomer;
import com.dlc.shop.bean.model.User;
import com.dlc.shop.bean.vo.ShopCustomerVO;
import com.dlc.shop.common.util.PageParam;
import com.dlc.shop.dao.ShopCustomerMapper;
import com.dlc.shop.service.ShopCustomerService;
import com.dlc.shop.service.UserService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 *
 *
 * @author LGH
 * @date 2023-01-12 10:37:54
 */
@Service
@AllArgsConstructor
public class ShopCustomerServiceImpl extends ServiceImpl<ShopCustomerMapper, ShopCustomer> implements ShopCustomerService {

    private final ShopCustomerMapper shopCustomerMapper;
    private final UserService userService;

    @Override
    public void saveCustomerByOrders(List<Order> orders) {
        if (CollectionUtils.isEmpty(orders)) {
            return;
        }
        String userId = orders.get(0).getUserId();
        User user = userService.getOne(new LambdaQueryWrapper<User>()
                .eq(User::getUserId, userId));
        // 获取订单中的店铺
        Set<Long> shopIds = orders.stream().map(Order::getShopId).collect(Collectors.toSet());
        // 找出数据库中该客户和该店铺关联数据
        List<ShopCustomer> shopCustomers = this.list(new LambdaQueryWrapper<ShopCustomer>()
                .eq(ShopCustomer::getUserId, userId)
                .in(ShopCustomer::getShopId, shopIds));
        Set<Long> dbShopId = new HashSet<>();
        if (!CollectionUtils.isEmpty(shopCustomers)) {
            dbShopId = shopCustomers.stream().map(ShopCustomer::getShopId).collect(Collectors.toSet());
        }
        if (shopIds.size() == dbShopId.size()) {
            return;
        }
        // 对不存在数据的店铺，进行新增
        List<ShopCustomer> customers = new ArrayList<>(16);
        ShopCustomer customer;
        for (Long shopId : shopIds) {
            if (!dbShopId.contains(shopId)) {
                customer = new ShopCustomer();
                customer.setUserId(userId);
                customer.setShopId(shopId);
                customer.setMemberName(user.getNickName());
                customer.setGrowth(0);
                customer.setLevelType(-1);
                customer.setCreateTime(new Date());
                customers.add(customer);
            }
        }
        this.saveBatch(customers);
    }

    @Override
    public IPage<ShopCustomerVO> pageShopCustomerByParam(PageParam<ShopCustomerVO> page, ShopCustomerDTO shopCustomer) {
        return shopCustomerMapper.pageShopCustomerByParam(page, shopCustomer);
    }

    @Override
    public ShopCustomerVO getIsShopCustomer(Long shopId, String userId) {
        return shopCustomerMapper.getIsShopCustomer(shopId, userId);
    }

}
